home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / mc302emb.zip / LIBCF / MALLOC.ASM < prev    next >
Assembly Source File  |  1994-03-18  |  3KB  |  116 lines

  1. *
  2. * Allocate a block of memory: char *malloc(size)
  3. *
  4. malloc    LDI    #?heap        Point to beginning of heap
  5. * Search for free block of memory
  6. ?1    LDB    I        Get flag
  7.     SJZ    ?4        End of list, allocate here
  8.     DEC            Un-allocated?
  9.     JNZ    ?2        No, try next
  10.     LD    1,I        Get size
  11. * Found free block, see if its large enough
  12.     CMP    2,S        Large enough?
  13.     UGE            Complete compare
  14.     SJNZ    ?3        Yes, its ok
  15. * This block not suitable, advance to next
  16. ?2    LD    1,I        Get size
  17.     ADDB    #3        Adjust for overhead
  18.     ADAI            Advance to next
  19.     SJMP    ?1        And keep looking
  20. * This block is OK to re-allocate
  21. ?3    LD    1,I        Get size back
  22.     SUB    2,S        Calculate remaining
  23.     ST    ?temp        Save new size
  24.     CMPB    #3        Is it worth splitting?
  25.     ULE            Complete compare
  26.     SJNZ    ?6        No, skip it
  27. * Split this block into two blocks
  28.     LD    2,S        Get size of block
  29.     PUSHI            Save index
  30.     ADAI            Offset to next
  31.     LDB    #1        Get released flag
  32.     STB    3,I        Set it free
  33.     LD    ?temp        Get size back
  34.     SUBB    #3        Convert
  35.     ST    4,I        Save size of block
  36.     LDI    S+        Restore INDEX
  37.     SJMP    ?5        And proceed
  38. * Allocate on end of memory
  39. ?4    TSA            Get stack pointer
  40.     SUB    2,S        Adjust for buffer size
  41.     SUB    #256        Adjust for margin
  42.     PUSHI            Save pointer
  43.     CMP    S+        Test it
  44.     UGT            Complete compare
  45.     SJZ    ?7        No, fail
  46. * Ok to proceed, allocate memory
  47.     LD    2,S        Get size
  48.     PUSHI            Save index
  49.     ADAI            Offset to new area
  50.     CLR            Get zero
  51.     STB    3,I        Indicate end of list
  52.     LDI    S+        Restore index
  53. ?5    LD    2,S        Get block size
  54.     ST    1,I        Save block size
  55. ?6    LDB    #2        Get 'Allocated' flag
  56.     STB    I        Set it
  57.     LEAI    3,I        Point to address
  58.     TIA            In ACC for return
  59. ?7    RET
  60. *
  61. * Release a block of memory: free(ptr)
  62. *
  63. free    LD    2,S        Get address
  64.     SUBB    #3        Backup to "real" beginning
  65.     PUSHA            Save for later
  66. * Search the allocation list for this block
  67.     LDI    #?heap        Point to beginning of heap
  68. ?10    LDB    I        Get address
  69.     SJZ    ?12        End of list
  70.     TIA            Get address
  71.     CMP    ,S        Is this it?
  72.     SJNZ    ?11        Yes, handle it
  73.     LD    1,I        Get size
  74.     ADAI            Advance for size
  75.     LEAI    3,I        Include overhead
  76.     SJMP    ?10        And keep looking
  77. * Mark this block as un-allocated
  78. ?11    LDB    #1        Get 'deallocated' flag
  79.     STB    I
  80. ?12    LD    S+        Clean up stack
  81. * Garbage collection, scan allocation list and join any
  82. * contiguous de-allocated blocks into single areas.
  83. * Also, truncate list at last allocated block.
  84.     LDI    #?heap        Point to beginning of heap
  85. ?20    LDB    I        Get allocation flag
  86.     SJZ    ?25        End, quit
  87.     DEC            Test for de-allocated
  88.     SJNZ    ?23        No, its not
  89.     LD    1,I        Get size of block
  90.     STI    ?temp        Save for later
  91. * This block is free, check following blocks
  92. ?21    ADAI            Point to next block
  93.     LDB    3,I        Get next flag
  94.     SJZ    ?24        End of list, its ok
  95.     DEC            Test for allocated?
  96.     SJNZ    ?22        Yes, stop looking
  97. * Next block is also free
  98.     LD    4,I        Get size of next block
  99.     ADDB    #3        Adjust for three
  100.     SJMP    ?21        And keep looking
  101. * Resave this block size
  102. ?22    TIA            Get end address
  103.     SUB    ?temp        Convert to actual size
  104.     LDI    ?temp        Get original pointer
  105.     ST    1,I        Save new size
  106. * Advance to next block in list
  107. ?23    LD    1,I        Get length
  108.     ADAI            Offset to next
  109.     LEAI    3,I        Include overhead
  110.     SJMP    ?20        And keep looking
  111. * Mark this block as end of list
  112. ?24    LD    ?temp        Get pointer
  113.     CLR            Get zero
  114.     STB    I        Indicate end of list
  115. ?25    RET
  116.